﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Abp.AspNetCore;
using Abp.AspNetCore.Configuration;
using Abp.Configuration.Startup;
using Abp.IO;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Web.SignalR;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Xugege.AbpExtension.Application.Extension;
using Xugege.AbpExtension.Com;
using Xugege.AbpExtension.Configuration;
using Xugege.AbpExtension.Core.Extension;
using Xugege.AbpExtension.Core.Extension.File;
using Xugege.AbpExtension.Web.Core.Extension.Controllers.Scripts.Angular;

namespace Xugege.AbpExtension.Web.Core.Extension
{

    [DependsOn(
        typeof(AbpZeroTemplateApplicationModule),
        typeof(AbpZeroTemplateApplicationExtensionModule),
        typeof(AbpZeroTemplateCoreModule),
        typeof(AbpZeroTemplateCoreExtensionModule),
        typeof(AbpAspNetCoreModule),
        typeof(AbpWebSignalRModule),
        typeof(AbpCommonLibModule))]
    public class AbpZeroTemplateWebCoreExtensionModule : AbpModule
    {

        private readonly IHostingEnvironment _env;
        private readonly IConfigurationRoot _appConfiguration;

        public AbpZeroTemplateWebCoreExtensionModule(IHostingEnvironment env)
        {
            _env = env;
            _appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName, env.IsDevelopment());
        }

        public override void PreInitialize()
        {
            base.PreInitialize();
            Configuration.Modules.AbpWebCommon().ApiProxyScripting.Generators[AngularProxyGenerator.Name] = typeof(AngularProxyGenerator);
            Configuration.Modules.AbpWebCommon().ApiProxyScripting.Generators[Angular2ProxyGenerator.Name] = typeof(Angular2ProxyGenerator);
            Configuration.Modules.AbpAspNetCore()
                .CreateControllersForAppServices(
                    typeof(AbpZeroTemplateApplicationExtensionModule).Assembly
                );
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }

        public override void PostInitialize()
        {
            SetAppFolders();

        }

        private void SetAppFolders()
        {
            var appFolders = IocManager.Resolve<AppFolderExtension>();
            appFolders.FileUploadPath = Path.Combine(_env.WebRootPath, @"Temp\Uploads");
            appFolders.WebRootPath = _env.WebRootPath;
        }
    }
}
